package com.chuantou.yuba.ui.widget;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import com.chuantou.yuba.R;
import com.chuantou.yuba.YBApp;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.FileUtil;
import com.chuantou.yuba.util.UiUtil;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

public class SharePopup extends PopupWindow implements OnClickListener {

	private View mContentView;
	private Activity mActivity;
	private Tencent mTencent;
	public static IWXAPI sWXApi;

	public SharePopup(Activity activity) {
		super(activity);
		mActivity = activity;
		mContentView = View.inflate(mActivity, R.layout.share_pop, null);
		mContentView.findViewById(R.id.share_wechat).setOnClickListener(this);
		mContentView.findViewById(R.id.share_pyq).setOnClickListener(this);
		mContentView.findViewById(R.id.share_qq).setOnClickListener(this);
		mContentView.setOnClickListener(this);
		setContentView(mContentView);
		setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(activity.getResources().getColor(R.color.translucence));
		setBackgroundDrawable(drawable);
	}

	public void dismissPopup() {
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.to_bottom);
		mContentView.clearAnimation();
		animation.setFillAfter(true);
		animation.setAnimationListener(new DismissAnimationListener() {

			@Override
			public void onAnimationEnd(Animation animation) {
				super.onAnimationEnd(animation);
				new Handler().post(new Runnable() {

					@Override
					public void run() {
						SharePopup.super.dismiss();
					}
				});
			}
		});
		mContentView.setAnimation(animation);
	}

	@Override
	public void dismiss() {
		dismissPopup();
	}

	@Override
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		super.showAsDropDown(anchor, xoff, yoff);
		startShowAnimation();
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		startShowAnimation();
	}

	private void startShowAnimation() {
		mContentView.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.from_bottom);
		animation.setFillAfter(true);
		mContentView.setAnimation(animation);
	}

	class DismissAnimationListener implements Animation.AnimationListener {

		private int position = -1;

		public DismissAnimationListener() {
		}

		public DismissAnimationListener(int position) {
			this.position = position;
		}

		public int getPosition() {
			return position;
		}

		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {

		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}

	}

	@Override
	public void onClick(View v) {
		if (UiUtil.isFastDoubleClick())
			return;
		switch (v.getId()) {
		case R.id.share_wechat:
			wxShare(SendMessageToWX.Req.WXSceneSession);
			break;
		case R.id.share_pyq:
			wxShare(SendMessageToWX.Req.WXSceneTimeline);
			break;
		case R.id.share_qq:
			qqShare();
			break;
		default:
			break;
		}
		dismiss();
	}

	private void wxShare(int reqScene) {
		if (sWXApi == null) {
			sWXApi = WXAPIFactory.createWXAPI(mActivity, Constants.WX_APP_ID, true);
			sWXApi.registerApp(Constants.WX_APP_ID);
			YBApp.sWXApi = sWXApi;
		}
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = mActivity.getString(R.string.share_url);
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = mActivity.getString(R.string.share_title);
		msg.description = mActivity.getString(R.string.share_content);
		Bitmap thumb = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.ic_launcher);
		msg.thumbData = FileUtil.bmpToByteArray(thumb, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene = reqScene;
		sWXApi.sendReq(req);
	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}

	private void qqShare() {
		final Bundle params = new Bundle();
		params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
		params.putString(QQShare.SHARE_TO_QQ_TITLE, mActivity.getString(R.string.share_title));
		params.putString(QQShare.SHARE_TO_QQ_SUMMARY, mActivity.getString(R.string.share_content));
		params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, mActivity.getString(R.string.share_url));
		File iconFile = FileUtil.copyResource2Sd(mActivity.getApplicationContext(), R.drawable.ic_launcher);
		params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, iconFile.getPath());
		params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mActivity.getString(R.string.app_name));
		if (mTencent == null) {
			mTencent = Tencent.createInstance(Constants.QQ_APP_ID, mActivity.getApplicationContext());
		}
		mTencent.shareToQQ(mActivity, params, mUiListener);
	}

	private IUiListener mUiListener = new IUiListener() {

		@Override
		public void onError(UiError arg0) {

		}

		@Override
		public void onComplete(Object arg0) {
		}

		@Override
		public void onCancel() {

		}
	};
}
